#ifndef JSONRPCMESSAGE_H
#define JSONRPCMESSAGE_H

#include <QString>
#include <QVariant>
#include <QJsonObject>

/**
 * @brief Base class for JSON-RPC 2.0 messages
 * 
 * This class provides the fundamental structure for JSON-RPC 2.0 messages
 * including required "jsonrpc": "2.0" field and optional "id" field.
 * The JSON object store in this class looks like:
 * ====================================
 * |   {                              |
 * |     jsonrpc:"2.0";               |
 * |     id?: string | number;        |
 * |   }                              |
 * ====================================
 */
class JsonRpcMessage
{
public:
    /**
     * @brief Construct a new JsonRpcMessage object
     */
    explicit JsonRpcMessage();

    /**
     * @brief Destroy the JsonRpcMessage object
     */
    virtual ~JsonRpcMessage();

    /**
     * @brief Get the JSON-RPC version
     * @return QString - Always returns "2.0"
     */
    QString jsonrpc() const;

    /**
     * @brief Get the message ID
     * @return QVariant - The message ID (can be QString, int, or null)
     */
    QVariant id() const { return mId; }

    /**
     * @brief Set the message ID
     * @param id - The message ID (must be QString)
     */
    void setId(const QString& id) { mId = id; }

    /**
     * @brief Set the message ID
     * @param id - The message ID (must numeric)
     */
    void setId(const double id) { mId = id; }

    /**
     * @brief Check if the message has an ID
     * @return bool - True if ID is valid, false otherwise
     */
    bool hasId() const { return mId.isValid() && !mId.isNull(); }

    /**
     * @brief Convert the message to JSON object
     * @return QJsonObject - The JSON representation of the message
     */
    virtual QJsonObject toJson() const = 0;

    /**
     * @brief Validate the message structure
     * @return bool - True if message is valid, false otherwise
     */
    virtual bool isValid() const = 0;

protected:
    QVariant mId;

private:
    static const QString JSON_RPC_VERSION;
};

#endif // JSONRPCMESSAGE_H
